#include "sdk.h"
#include "debuggersettingspanel.h"
#include <wx/string.h>
#include <wx/intl.h>
#include <wx/textdlg.h>
#include "debuggermanager.h"
#include "debuggersettingsdlg.h"

const long DebuggerSettingsPanel::ID_BUTTON_CREATE = wxNewId();
const long DebuggerSettingsPanel::ID_BUTTON_DELETE = wxNewId();
const long DebuggerSettingsPanel::ID_BUTTON_RESET = wxNewId();
const long DebuggerSettingsPanel::ID_TEXTCTRL_INFO = wxNewId();

BEGIN_EVENT_TABLE( DebuggerSettingsPanel, wxPanel )
END_EVENT_TABLE()

DebuggerSettingsPanel::DebuggerSettingsPanel( wxWindow* parent, DebuggerSettingsDlg *dialog, cbDebuggerPlugin *plugin ) :
  m_dialog( dialog ),
  m_plugin( plugin ) {
  wxBoxSizer* buttonSizer;
  wxTextCtrl* textInfo;
  wxButton* butReset;
  wxButton* butCreate;
  wxBoxSizer* mainSizer;
  wxStaticBoxSizer* infoSizer;
  wxButton* butDelete;
  Create( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T( "wxID_ANY" ) );
  mainSizer = new wxBoxSizer( wxVERTICAL );
  buttonSizer = new wxBoxSizer( wxHORIZONTAL );
  butCreate = new wxButton( this, ID_BUTTON_CREATE, _T( "创建配置" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON_CREATE" ) );
  buttonSizer->Add( butCreate, 1, wxALIGN_BOTTOM, 5 );
  butDelete = new wxButton( this, ID_BUTTON_DELETE, _T( "删除配置" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON_DELETE" ) );
  buttonSizer->Add( butDelete, 1, wxLEFT | wxALIGN_BOTTOM, 5 );
  butReset = new wxButton( this, ID_BUTTON_RESET, _T( "重置默认" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON_RESET" ) );
  buttonSizer->Add( butReset, 1, wxLEFT | wxALIGN_BOTTOM, 5 );
  mainSizer->Add( buttonSizer, 0, wxALL | wxEXPAND | wxSHAPED, 0 );
  infoSizer = new wxStaticBoxSizer( wxHORIZONTAL, this, _T( "信息" ) );
  textInfo = new wxTextCtrl( this, ID_TEXTCTRL_INFO, wxEmptyString, wxDefaultPosition, wxSize( 186, 243 ), 0, wxDefaultValidator, _T( "ID_TEXTCTRL_INFO" ) );
  textInfo->Disable();
  infoSizer->Add( textInfo, 1, wxEXPAND, 5 );
  mainSizer->Add( infoSizer, 1, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, 0 );
  SetSizer( mainSizer );
  mainSizer->Fit( this );
  mainSizer->SetSizeHints( this );
  Connect( ID_BUTTON_CREATE, wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&DebuggerSettingsPanel::OnButtonCreate );
  Connect( ID_BUTTON_DELETE, wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&DebuggerSettingsPanel::OnButtonDelete );
  Connect( ID_BUTTON_RESET, wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&DebuggerSettingsPanel::OnButtonReset );
  if( plugin && plugin->IsRunning() ) {
    butCreate->Disable();
    butDelete->Disable();
    butReset->Disable();
  }
}

DebuggerSettingsPanel::~DebuggerSettingsPanel() {
}

void DebuggerSettingsPanel::OnButtonCreate( M_Code_unused wxCommandEvent& event ) {
  wxTextEntryDialog dialog( this, _T( "请指定配置的名称" ), _T( "Create config" ) );
  PlaceWindow( &dialog );
  while( dialog.ShowModal() == wxID_OK ) {
    wxString name = dialog.GetValue();
    if( !name.empty() ) {
      if( m_dialog->CreateConfig( this, m_plugin, name ) ) {
        break;
      } else
      { InfoWindow::Tip( _T( "名称冲突" ), _T( "错误" ), 2500 ); }
    }
  }
}

void DebuggerSettingsPanel::OnButtonDelete( M_Code_unused wxCommandEvent& event ) {
  m_dialog->DeleteConfig( this, m_plugin );
}

void DebuggerSettingsPanel::OnButtonReset( M_Code_unused wxCommandEvent& event ) {
  if( msging( _T( "重置所选插件的设置??" ), _T( "重置设置" ), wxICON_QUESTION | wxYES ) == wxID_NO ) {
    return;
  }
  m_dialog->ResetConfig( this, m_plugin );
}
